<?php

namespace app\models\system;

use app\models\Images;
use Yii;

/**
 * This is the model class for table "set_merchant".
 *
 * @property int $id
 * @property int $qrcode_background 线下码背景
 */
class SetMerchant extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'set_merchant';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['qrcode_background'], 'integer'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'qrcode_background' => 'Qrcode Background',
        ];
    }

    public static function getLastOne($isObject = true)
    {
        if ($isObject) {
            $model = self::find()
                ->orderBy(['id' => SORT_DESC])
                ->one();
        } else {
            $model = self::find()
                ->orderBy(['id' => SORT_DESC])
                ->with(['image'])
                ->asArray()
                ->one();
        }

        if (!$model) {
            $model = new self();
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['SetMerchant' => $data]) && $model->save()) {
            return true;
        }
    }

    public function getImage()
    {
        return $this->hasOne(Images::class, ['id' => 'qrcode_background']);
    }
}
